<template>
  <div>
    <dt-modal-edit ref='modal' :title='title' :label-width='130' :width='500' @ok='onOk'
      :loading='loading'>
    </dt-modal-edit>
  </div>
</template>

<script>
  import Opt from '../opt/addCompanyOpt.js'
  import companyApi from '@/biz/backStage/companyCard'

  export default {
    data() {
      return {
        info: {
          companyName: '',
          orgCompanyId: null,
          orgDivisionId: null,
          orgOfficeId: null,
          address: '',
          waterMark: '2'
        },
        loading: false,
        title: '',
        flag: ''
      }
    },
    mounted() {},
    methods: {
      show(data) {
        this.title = data.title
        data.orgCompanyId = data.orgCompanyId ? typeof(data.orgCompanyId) != 'string' ?
          String(data.orgCompanyId).split(',') : data.orgCompanyId.split(',') : []
        this.info = Object.assign(this.info, data)
        this.flag = data.flag
        this.$refs.modal.show(new Opt(this.info))
      },
      hide() {
        this.$refs.modal.hide()
      },
      onOk(data) {
        console.log(data, 'submitDatax')
        this.loading = true
        let params = {
          ...data
        }
        params.orgCompanyId = params.orgCompanyId ? params.orgCompanyId.join(',') :
          null
        params.orgDivisionId = params.orgDivisionId ? params.orgDivisionId : null
        params.orgOfficeId = params.orgOfficeId ? params.orgOfficeId : null
        if (this.flag == 'edit') {
          companyApi.companyUpdate(params).then(res => {
            this.loading = false
            this.$emit('success')
            this.hide()
          }).catch(err => {
            this.loading = false
          })
        } else if (this.flag == 'add') {
          companyApi.companyCreate(params).then(res => {
            if (res) {
              this.loading = false
              this.hide()
              this.$emit('success')
            }
          }).catch(err => {
            this.loading = false
          })
        }
      }
    }
  }
</script>